package cn.iocoder.yudao.module.mqtt.framework.mqtt;

import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;

public class LoggingMqttPahoClientFactory extends DefaultMqttPahoClientFactory {
    private final String clientId;
    private final MqttCallback callback;
    private MqttClient cachedClient; // 新增

    public LoggingMqttPahoClientFactory(MqttConnectOptions options, String clientId, MqttCallback callback) {
        super();
        this.setConnectionOptions(options);
        this.clientId = clientId;
        this.callback = callback;
    }

    @Override
    public synchronized MqttClient getClientInstance(String uri, String clientId) throws MqttException {
        if (cachedClient == null) {
            cachedClient = (MqttClient) super.getClientInstance(uri, clientId);
            cachedClient.setCallback(callback);
        }
        return cachedClient;
    }

    // 新增
    public MqttClient getCachedClient() {
        return cachedClient;
    }
} 